<?php

/**
 * 财务页面
 */
namespace Financial\Controller;
use Common\Controller\AdminbaseController;

class FinancialadminController extends AdminbaseController
{
    private $model = null;

    private $data = array();

    function _initialize() {
    }

    function index()
    {
        $this->lists();
        $this->display();
    }

    function lists()
    {
        $type = I("type");
        $this->model =new \Common\Model\FinancialModel();
        $total =intval($this->model->getOrderTotal($type));
        $page = new \Think\Page($total,10);
        $list = $this->model->getOrderInfoByPage($page->firstRow,$page->listRows,$type);
        $data = array();
        $total_money = 0;
        $status = C('orderStatus');
        foreach($list as $k =>$v)
        {
            $data[$k]['id'] = $v['orderId'];
            $data[$k]['num'] = $v['order_num'];
            $data[$k]['cTime'] = $v['cTime'];
            $data[$k]['uid'] = $v['uid'];
            $data[$k]['status'] = $status[$v['status']];
            $data[$k]['status_time'] = empty($v['status_time']) ? $v['cTime'] : $v['status_time'];
            $data[$k]['posted'] = $v['posted'];
            $post_info = $this->model->getPostedNum($v['orderId']);
            $data[$k]['num_info'] = empty($post_info['num_info']) ? (empty($v['posted']) ? '': '<b style="color: #ff0000">订单已发货，暂无发货单号！请尽快处理</b>') : $post_info['num_info'];
            $data[$k]['class_type'] = (int)($k%2);
            $data[$k]['total'] = $v['total'];
            //获取商品信息
            $goods = $this->model->getGoodsListByPids(implode(',',explode('-',$v['pid'])));
            $data[$k]['goods_num'] = count($goods);
            $data[$k]['goodsInfo'] = '';
            if(empty($goods))
            {
                $data[$k]['goodsInfo'] = '<b style="color: #ff0000">订单商品信息获取失败！</b>';
            }
            $size = C('goodsSize');
            foreach($goods as $key=>$value)
            {
                $data[$k]['goodsInfo'] .= '<b>商品'.($key+1).'</b>: 尺寸：'.$size[$key].'数量：'.$value['num'].'&nbsp;&nbsp;';
            }
            $data[$k]['would_status'] = $this->orderStatusWould($v['cTime']);
            //当页订单总价
            $total_money += $v['total'];
        }

        $show = $page->show();
        $this->assign("page",$show);
        $this->assign("total_money",$total_money);
        $this->assign('data',$data);
    }

    function orderStatusWould($time)
    {
        $would_time = time() - $time;
        $passDay = intval($would_time/(3600*24));
        $status = C('orderStatus');
        switch ($passDay)
        {
            case 0 :
                $wStatus = $status[1];
                break;
            case 1 :
            case 2 :
                $wStatus = $status[2];
                break;
            case 3 :
            case 4 :
            case 5 :
            case 6 :
            case 7 :
            case 8 :
                $wStatus = $status[3];
                break;
            case 9 :
            case 10:
                $wStatus = $status[4];
                break;
            case 11 :
                $wStatus = $status[7];
                break;
            default :
                $wStatus = $status[0];
                break;
        }
        return $wStatus;
    }

    function show()
    {
        $id = intval(I('id'));
        if(empty($id))
        {
            $this->display();
            exit;
        }
        $this->model = new \Common\Model\FinancialModel();
        //订单信息
        $order = $this->model->getOrderInfoById($id);
        //订单预计状态
        $order['would_status'] = $this->orderStatusWould($order['cTime']);
        //订单状态
        $status = C('orderStatus');
        $order['status'] = $status[$order['status']];
        if(empty($order))
        {
            $this->error('订单信息不存在！');
        }
        $good = $this->model->getGoodsListByPids(implode(',',explode('-',$order['pid'])));
        if(empty($good))
        {
            $this->error('商品信息不存在！');
        }
        $size = C('goodsSize');
        foreach($good as $k=>$v)
        {
            $order['goodsInfo'] .= '<b>商品'.($k+1).'</b>: 尺寸：'.$size[$v['size']].'数量：'.$v['num'].'&nbsp;&nbsp;';
            $good[$k]['size'] = $size[$v['size']];
            $hd_url = $this->model->getHdImage($v['hd_id']);
            $good[$k]['hd_id'] = $hd_url['url'];
            $bd_url = $this->model->getBdImage($v['bd_id']);
            $good[$k]['bd_id'] = $bd_url['url'];
            $good[$k]['class_type'] = (int)($k%2);
        }
        $post = $this->model->getPostInfoById($order['postId']);
        $post['addrInfo'] = $post['province'].$post['city'].$post['zone'].$post['addr'];
        $userImage = $this->model->getUserImageList($order['uid']);
        $userImage = unserialize($userImage['user_image']);
        $user = $this->model->getUserInfo($order['uid']);

        $this->assign('order',$order);
        $this->assign('good',$good);
        $this->assign('image',$userImage);
        $this->assign('user',$user);
        $this->assign('post',$post);
        $this->display();
    }

    function statistics()
    {
        $this->display();
    }

    function search()
    {
        if(!IS_POST) $this->error('非法操作');
        $type = intval(I('type'));
        $start = I('start_time');
        $end = I('end_time');
        $this->model = new \Common\Model\FinancialModel();

        if(empty($start) || empty($end))
        {
            $this->error('请选择开始和结束日期');
        }
        $size = C('goodsSize');
        if($type === 1)
        {
            //选取的天数,用于x轴显示
            $day_num = (strtotime($end)-strtotime($start))/(24*3600);
            $i = 0;
            while($i<=$day_num)
            {
                $xInfo[$i] = date('m-d',(strtotime($start)+$i*24*3600));
                //一天的订单详情
                $data[$xInfo[$i]]['order_day'] = $this->model->getOrderNumDay((strtotime($start)+($i+1)*24*3600));

                //一天的订单总数
                $day_order_num[$i] = count($data[$xInfo[$i]]['order_day']);
                $data[$xInfo[$i]]['day_order_num'] = $day_order_num[$i];
                //一天的总金额
                $day_t = $this->model->getOrderTotalDay((strtotime($start)+($i+1)*24*3600));
                $data[$xInfo[$i]]['day_total'] = empty($day_t) ? 0 : $day_t;
                $day_total[$i] =  round(floatval($data[$xInfo[$i]]['day_total']),2);
                $data[$xInfo[$i]]['day_total'] = round(floatval($data[$xInfo[$i]]['day_total']),2);
                $num = array(0,0,0);
                foreach($data[$xInfo[$i]]['order_day'] as $k => $v)
                {
                    for($p=0;$p<3;$p++)
                    {
                        $type_num = $this->model->getGoodsListByPidsAndType(implode(',', explode('-', $v['pid'])),$p);
                        if (!empty($type_num))
                        {
                            foreach($type_num as $ke=>$va)
                            {
                                $num[$p] = $num[$p] + $va['num'];
                            }
                        }
                    }
                }
                //每天的具体分类数
                $data[$xInfo[$i]]['type_num_day'] = $num;
                foreach($data[$xInfo[$i]]['type_num_day'] as $kee=> $vaa)
                {
                    $data[$xInfo[$i]]['type_day'] .= '<b>类型'.$size[$kee].'</b>:<b style="color:red; font-size:18px;">'.$vaa.'</b>&nbsp;&nbsp;';
                }
                $i++;
            }

            $this->assign('data',$data);
            $this->assign('total',round(floatval(array_sum($day_total)),2));
            $this->assign('num_total',array_sum($day_order_num));
            $this->display('statistics');
        }
        elseif($type === 0)
        {
            $day_order = $this->model->getOrderNumDay(strtotime($start),strtotime($end));
            $total =intval($this->model->getOrderTotal($type));
            $page = new \Think\Page($total,10);
            $list = $this->model->getOrderInfoByPage($page->firstRow,$page->listRows,$type);
            $data = array();
            $total_money = 0;
            $status = C('orderStatus');
            foreach($list as $k =>$v)
            {
                $data[$k]['id'] = $v['orderId'];
                $data[$k]['num'] = $v['order_num'];
                $data[$k]['cTime'] = $v['cTime'];
                $data[$k]['uid'] = $v['uid'];
                $data[$k]['status'] = $status[$v['status']];
                $data[$k]['status_time'] = empty($v['status_time']) ? $v['cTime'] : $v['status_time'];
                $data[$k]['posted'] = $v['posted'];
                $post_info = $this->model->getPostedNum($v['orderId']);
                $data[$k]['num_info'] = empty($post_info['num_info']) ? (empty($v['posted']) ? '': '<b style="color: #ff0000">订单已发货，暂无发货单号！请尽快处理</b>') : $post_info['num_info'];
                $data[$k]['class_type'] = (int)($k%2);
                $data[$k]['total'] = $v['total'];
                //获取商品信息
                $goods = $this->model->getGoodsListByPids(implode(',',explode('-',$v['pid'])));
                $data[$k]['goods_num'] = count($goods);
                $data[$k]['goodsInfo'] = '';
                if(empty($goods))
                {
                    $data[$k]['goodsInfo'] = '<b style="color: #ff0000">订单商品信息获取失败！</b>';
                }
                $size = C('goodsSize');
                foreach($goods as $key=>$value)
                {
                    $data[$k]['goodsInfo'] .= '<b>商品'.($key+1).'</b>: 尺寸：'.$size[$key].'数量：'.$value['num'].'&nbsp;&nbsp;';
                }
                $data[$k]['would_status'] = $this->orderStatusWould($v['cTime']);
                //当页订单总价
                $total_money += $v['total'];
            }

            $show = $page->show();
            $this->assign("page",$show);
            $this->assign("total_money",$total_money);
            $this->assign('data',$data);

        }

    }
    function md5Pass()
    {
        $pass = 'admin';
        $pre = 'dob_';

        var_dump(sp_password($pass,$pre));exit;
    }

}
